package mop.model.task.field;

import mop.exception.IllegalDescriptionException;
import mop.exception.IllegalIdException;
import mop.exception.IllegalValueException;

/**
 * IntField Class
 * 
 * @author MOP Team 7
 */
public class IntField extends Field
{

	/**
	 * Constructor for Intfield class 
	 * @see Field#Field(String, String)
	 */
	public IntField(String id, String description) throws IllegalIdException, IllegalDescriptionException
	{
		super(id, description);
	}
	
	/**
	 * Copy / Clone
	 * 
	 * @param the field to copy
	 * 
	 * @return a copy of this object
	 */
	public IntField clone() 
	{
		IntField n = null;
		try
		{
			n = new IntField(getId(),getDescription());
			n.setValue(getValue());
		}
		catch(Throwable e)
		{
			//impossible at this point
		}
		
		return n;
	}
	
	/***************************************************************************
	 * 						Value
	 ***************************************************************************/
	
	/**
	 * This is were the magic happens, the passed string
	 * is converted to int
	 * 
	 * @param value the value in string form 
	 * 
	 * @throws IllegalValue Exception
	 * 	when the passed string can not be converted to a valid integer
	 */
	public void setValue(String value) throws IllegalValueException
	{
		try
		{
			this.value = Integer.parseInt(value);
		}
		catch(NumberFormatException e)
		{
			this.value = null;
			throw new IllegalValueException(value,e.toString());
		}
	}
	
	/**
	 * Returns the current value in String form
	 * to make it easier for our gui
	 * 
	 * @return String value
	 */
	public String getValue()
	{
		if(value == null) return "(null)";
		else
		{
			return Integer.toString(this.value);
		}
	}
	
	/**
	 * the value being hold
	 */
	private Integer value = null;
	
	/***************************************************************************
	 * 						Model interface
	 ***************************************************************************/
	
	/**
	 * get a reference to the interface
	 * @return this
	 */
	public FieldInterface getInterface()
	{
		return this;
	}

        /**
         * return a string view
         * @return this.id - this.description - this.value
         */
        public String toString()
        {
            String a = this.getId()+ "-" +this.getDescription()+"-"+this.getValue();
            return a;
        }
	
}
